<template>

  <div class="addMerchantBox" v-loading="loading">
    <el-form ref="form" :model="initial_data" :rules="rules">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="initial_data.username" placeholder="请输入用户名" style="width:300px"></el-input>
      </el-form-item>

      <el-form-item label="密码">
        <el-input v-model="initial_data.password" placeholder="不修改密码则不填写" style="width:300px"></el-input>
      </el-form-item>

      <el-form-item label="联系人" prop="real_name">
        <el-input v-model="initial_data.real_name" placeholder="请填写联系人" style="width:300px"></el-input>
      </el-form-item>

      <el-form-item label="	手机号" prop="mobile">
        <el-input v-model="initial_data.mobile" placeholder="请填写手机号" style="width:300px"></el-input>
      </el-form-item>

      <el-form-item label="	企业名称" prop="company">
        <el-input v-model="initial_data.company" placeholder="请填写企业名称" style="width:300px"></el-input>
      </el-form-item>

      <el-form-item label="	主营类型" prop="main_type">
        <el-input v-model="initial_data.main_type" placeholder="请填写主营类型" style="width:300px"></el-input>
      </el-form-item>
    </el-form>

    <div class="submitBtnBox">
      <el-button class="submitBtn" @click="submitFn" type="primary">保存</el-button>
      <el-button class="submitBtn" @click="submituser" v-if="showVisble" type="primary">账号认证</el-button>
    </div>
  </div>
</template>

<script>

export default {
  name: "setting-basic",
  components: {},
  data() {
    return {
      initial_data: {
        //获取初始数据
        username: "",
        password: "",
        real_name: "",
        mobile: "",
        company: "",
        type: ""
      },
      loading: false,
      showVisble: false,
      rules: {
        username: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        real_name: [{ required: true, message: "请输入账号", trigger: "blur" }],
        mobile: [{ required: true, message: "请上传头像", trigger: "blur" }],
        company: [{ required: true, message: "请选择角色", trigger: "blur" }],
        main_type: [{ required: true, message: "请选择权限", trigger: "blur" }]
      }
    };
  },
  watch: {},
  methods: {
    //用户信息
    getInfo() {
      this.loading = true
      this.$http.get("/agent/info/msg", {
          params: {
            token: localStorage.getItem("user_token"),
            id: localStorage.getItem('user_id')
          }
        }).then(response => {
          if (response.data.status == "success") {
            this.initial_data = response.data.data;
            this.initial_data.token = localStorage.getItem("user_token"),
            this.initial_data.id = localStorage.getItem("user_id"),
            this.initial_data.password = ""

            if (response.data.data.company && response.data.data.main_type) {
              this.showVisble = true
            }
            this.loading = false
          }
        })
        .catch(error => {
          console.log("error:", error);
          this.loading = false
        });
    },

    submitFn() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false;
        } else {
          this.loading = true;
          this.$nextTick(() => {
            this.$http.post("/agent/info/usave/msg", this.initial_data).then(response => {
                if (response.data.status == "success") {
                  this.$message({
                    message: "保存成功",
                    duration: 6000
                  });

                  this.getInfo()
                } else {
                  this.$message({
                    message: "保存失败，原因：" + response.data.failedMsg,
                    duration: 6000
                  });
                }
                  this.loading = false;
              })
              .catch(error => {
                this.$message({
                  message: "保存失败，原因：" + error,
                  duration: 6000
                });
                this.loading = false;
              });
          });
        }
      });
    },

    submituser(){
      this.loading = true;
      this.$nextTick(() => {
            this.$http.get("/agent/info", {params:{id :this.initial_data.id, token: this.initial_data.token}})
            .then(response => {
                if (response.data.status === "success") {
                  
                  if (response.data.data.status === null) {
                    this.$router.push({name: 'certification'})
                  }
                  if (response.data.data.status === 0) {
                    this.$alert(
                      '信息已经提交，3个工作日内，平台工作人员会与您联系核实信息。请您保持电话畅通。',
                      '等待审核',
                      {confirmButtonText: '确定'}
                    )
                  }

                  this.getInfo()
                }
                  this.loading = false;
              })
              .catch(error => {
                  console.log(error)
                this.loading = false;
              });
          });
    }
  },
  mounted() {
    this.getInfo();
  }
};
</script>

<style scoped lang="scss">
@import "./setting.scss";
</style>
